package com.earl.eatbao.service.impl;

import javax.annotation.Resource;

import com.earl.eatbao.commons.dao.BaseRepsitoty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.earl.eatbao.commons.service.BaseServiceImpl;
import com.earl.eatbao.dao.eatbao.CommentDao;
import com.earl.eatbao.domain.eatbao.Comment;
import com.earl.eatbao.service.CommentService;

import java.util.Date;
import java.util.List;

@Service("commentService")
public class CommentServiceImpl extends BaseServiceImpl<Comment,Long> implements CommentService {

	// desc:日记工具logger
	private static final Logger logger = LoggerFactory.getLogger(CommentServiceImpl.class);

    @Resource
    private CommentDao commentDao;

    @Override
    protected BaseRepsitoty<Comment, Long> getDao() {
        return commentDao;
    }

	@Override
	public Comment findOneByOrderId(Long orderId) {
		logger.info("<====== 进入findOneByOrderId:方法,{[orderId] :" + orderId + " } ====");
		Comment comment = commentDao.findOneByOrderId(orderId);
		logger.info("==== 退出findOneByOrderId方法 返回值类型:{ Comment: } =======>");
		return comment;
	}

    @Override
    public List<Comment> findUserComments(Long userId) {
        logger.info("<====== 进入findUserComments:方法,{[userId] :" + userId + " } ====");

        if (null != userId && userId != 0l) {
            logger.info("==== 退出findUserComments方法 返回值类型:{ List<Comment>: } =======>");
            return commentDao.findByUserId(userId);
        } else {
            logger.info("==== 退出findUserComments方法 返回值类型:{ List<Comment>: 输入不和法 } =======>");
            return null;
        }
    }

    @Override
    public List<Comment> findBusinessComments(Long businessId) {
        logger.info("<====== 进入findBusinessComments:方法,{[businessId] :" + businessId + " } ====");
        if (null != businessId) {
            logger.info("==== 退出findBusinessComments方法 返回值类型:{ List<Comment>: } =======>");
            return commentDao.findByBusinessId(businessId);
        } else {
            logger.info("==== 退出findBusinessComments方法 返回值类型:{ List<Comment>: null } =======>");
            return null;
        }
    }

    @Override
    public Boolean deleteComments(Long commentId) {
        logger.info("<====== 进入deleteComments:方法,{[commentId] : " + commentId + " } ====");
        Boolean result = false;
        if (null != commentId && commentId != 0l) {
            commentDao.delete(commentId);
            result = true;
        } else {
            logger.info("=== 提交的评论ID不合法 ===");
        }
        logger.info("==== 退出deleteComments方法 返回值类型:{ java.lang.Boolean: " + result + " } =======>");
	    return null;
    }

    @Override
    public Boolean commentsOrders(Comment comment) {

        logger.info("<====== 进入commentsOrders:方法,{[comment] :" + comment.toString()
                + " } ====");
        logger.info("==== 退出commentsOrders方法 返回值类型:{ Boolean:} =======>");
        return addComment(comment);
    }

    @Override
    public Boolean reventComment(Comment comment) {
        logger.info("<====== 进入reventComment:方法,{[comment] :"+ comment.toString()
                + " } ====");
        logger.info("==== 退出reventComment方法 返回值类型:{ Boolean: } =======>");
        return addComment(comment);
    }

    /**
     * 添加一条订单评论
     * @param comment
     * @return
     */
    private Boolean addComment(Comment comment) {
        if (null != comment  && comment.getOrderId() != 0l
                && comment.getUserId() != 0l
                && comment.getContent().trim().length() > 0 ) {
            comment.setCreatedBy(String.valueOf(comment.getUserId()));
            comment.setLastModifiedBy(String.valueOf(comment.getUserId()));
            comment.setCreatedDate(new Date());
            comment.setLastModifiedDate(new Date());
            comment.setDelete(false);
            commentDao.save(comment);
            return true;
        }  else {
            logger.info("==== 评论不合法 ====");
            return false;
        }
    }
}